/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.acionista.view;

import br.jm.acionista.base.eao.EaoException;
import br.jm.acionista.entity.Cautela;
import br.jm.acionista.entity.Cliente;
import br.jm.acionista.sistema.eao.CautelaEao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author joao
 */
public class RelatorioVendasBean implements Serializable {
private static final long serialVersionUID = 1L;
    private Cautela selectedCautela;

    private CautelaEao CautelaJpa;
    private LazyDataModel<Cautela> lazyModelCautela;
    
    private FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Relatório Gerado com Sucesso!", "");
    private int nTotalReg;
    private String imppartic = "S";
    private String impproposta = "S";
    private String impobs = "S";
    private FacesContext ctx;
    public HttpSession session;
    private Date datrel = new Date();
    
    public RelatorioVendasBean() throws EaoException {
        CautelaJpa = CautelaEao.getInstance();
        selectedCautela = new Cautela();
        selectedCautela.setDatCautela(new Date());
    
        // Grid Transferencia
        lazyModelCautela = new LazyDataModel<Cautela>() {

            @Override
            public List<Cautela> load(int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> filters) {

                List<Cautela> lazyCautela = new ArrayList<Cautela>();

                try {
                    lazyCautela = CautelaJpa.getNative("Cautela", first, pageSize, sortField, sortOrder, filters, null, "cautela.id_tipo = 1 ");
                    
                    Cliente cliente1 = new Cliente();
                    Cliente cliente2 = new Cliente();
                    List<Cautela> lazyCautela2 = new ArrayList<Cautela>();
                    
                    for (int i = 0; i < lazyCautela.size(); i++) {
                       cliente1 = lazyCautela.get(i).getIdCliente();
                       if (cliente1.equals(cliente2)) {
                           selectedCautela = lazyCautela.get(i);
                           lazyCautela2.add(selectedCautela);
                       } else {
                          cliente2 = cliente1; 
                       }
                    }
                    boolean remove = lazyCautela.removeAll(lazyCautela2);
                } catch (Exception ex) {
                    Logger.getLogger(CautelaBean.class.getName()).log(Level.SEVERE, null, ex);
                }
                return lazyCautela;
            }
        };

        nTotalReg = CautelaJpa.getCount("Cautela");
        if (nTotalReg == 0) {
            nTotalReg = 1;
            selectedCautela = new Cautela();
        }
        lazyModelCautela.setRowCount(nTotalReg);
    }

    public LazyDataModel<Cautela> getLazyModelCautela() {
        return lazyModelCautela;
    }

    public void setLazyModelCautela(LazyDataModel<Cautela> lazyModelCautela) {
        this.lazyModelCautela = lazyModelCautela;
    }

    public Cautela getSelectedCautela() {
        if (selectedCautela == null) {
            selectedCautela = new Cautela();
        }
        return selectedCautela;
    }

    public void setSelectedCautela(Cautela selectedCautela) {
        this.selectedCautela = selectedCautela;
    }

    public String getImpobs() {
        return impobs;
    }

    public void setImpobs(String impobs) {
        this.impobs = impobs;
    }

    public String getImppartic() {
        return imppartic;
    }

    public void setImppartic(String imppartic) {
        this.imppartic = imppartic;
    }

    public String getImpproposta() {
        return impproposta;
    }

    public void setImpproposta(String impproposta) {
        this.impproposta = impproposta;
    }

    public void gerarVenda(ActionEvent actionEvent) {
        try {
            selectedCautela.setDatCautela(datrel);
            
            ctx = FacesContext.getCurrentInstance();
            session = (HttpSession) ctx.getExternalContext().getSession(false);
            session.setAttribute("cautela", selectedCautela);
            session.setAttribute("impPartic", imppartic);
            session.setAttribute("impProposta", impproposta);
            session.setAttribute("impObs", impobs);
            
            for (int i = 0; i < 600000000; i++) {
                String cTeste = "";
            }
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Relatório Gerado com Sucesso!", "");
        } catch (Exception ex) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Erro na Geração do Relatório!!", "");
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public Date getDatrel() {
        return datrel;
    }

    public void setDatrel(Date datrel) {
        this.datrel = datrel;
    }
    
}
    

